package cn.ruituo.server;

import cn.ruituo.common.Request;
import cn.ruituo.common.Response;
import cn.ruituo.common.User;

import java.io.*;
import java.net.Socket;

/**
 * @author <作者>
 * @since <版本>
 */
public class ServerThread extends Thread {
    private final Socket s;
    private static UserManager userManager = new UserManager();

    public ServerThread(Socket s) {
        this.s = s;
    }

    public Response download(Request request) {
        String novelName = (String) request.get("novelname");
        return new Response(false, "此小说不存在");
    }

    /**
     * 传输格式：内容用byte[],加上一个文件名
     *
     * @param request
     * @return
     */
    public Response upload(Request request) {

        String novelName = (String) request.get("novelname");
        byte[] b = (byte[]) request.get("content");
        File f = new File("E:\\temp", novelName);
        try {
            FileOutputStream fout = new FileOutputStream(f);
            fout.write(b);
            fout.close();
            return new Response(true, "此小说上传成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Response(false, "此小说信息有错误");
    }

    public Response login(Request request) {
        String name = (String) request.get("userName");
        String pass = (String) request.get("userPass");

        User user = userManager.login(name, pass);
        if (user == null) {
            return new Response(false, "账号或者密码错误");
        }
        Response response = new Response(true);
        response.put("loginUser", user);
        return response;
    }

    //    public Response login(Request request){
//        String name=(String)request.get("userName");
//        String pass=(String)request.get("userPass");
//        if(name.startsWith("java72")&&pass.equals("123456"))
//            return new Response(true);
//
//        return new Response(false,"账号或者密码错误");
//    }
    public Response register(Request request) {
        String name = (String) request.get("userName");
        String pass = (String) request.get("userPass");
        if (name.equals("admin") || pass.length() < 6)
            return new Response(false, "注册信息不合法");

        return new Response(true, "注册成功");
    }

    /**
     * 退出
     *
     * @param request
     * @return
     */
    public Response quit(Request request) {
        return new Response(true);
    }

    @Override
    public void run() {
        //记录是否登录，默认为False，没有登录
        boolean isLogin = false;
        try {
            System.out.println(s + "连接成功！");
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            ObjectInputStream oin = new ObjectInputStream(in);
            ObjectOutputStream oout = new ObjectOutputStream(out);
            while (true) {
                Request request = (Request) oin.readObject();
                String act = request.getAction();
                Response response = null;
                //确定哪些请求不需要登录
                if (act.equals("login")) {
                    response = login(request);
                    isLogin = response.isSuccess();//
                } else if (act.equals("register")) {
                    response = register(request);
                } else if (!isLogin) {//以下的操作必须登录后进行
                    response = new Response(false, "您还没有登录，不能进行此操作");
                } else {
                    if (act.equals("download")) {
                        response = download(request);
                    } else if (act.equals("upload")) {
                        response = upload(request);
                    } else if (act.equals("quit")) {
                        response = quit(request);
                        isLogin=false;//重置
                    } else {
                        //功能待实现
                        response = new Response(false, "此功能没有实现");
                    }
                }
                //
                oout.writeObject(response);
                oout.flush();
                oout.reset();//重置，防止发上次的对象
                //单独处理Quit
                if (act.equals("quit"))
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                isLogin=false;//重置
                s.close();
                System.out.println(s + "退出。。。");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
